# 4 字典
## 4.1 空字典，添加、删除键值对
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
alien_0['height'] = 185
# print(alien_0)

# del alien_0['height']
# print(alien_0)

## 4.2 用get()来访问值
# point_value = alien_0.get('height', 'No height value assigned.')
# point_value_ex = alien_0.get('height')
# print(point_value)
# print(point_value_ex)

## 4.3 遍历字典的键值对
# for key, value in alien_0.items():
#     print(f"\nKey: {key}")
#     print(f"Value: {value}")

# for key in alien_0.keys(): # 显式
# for key in alien_0: # 隐式（遍历字典时，默认遍历所有键）
#     print(key.title())

## 4.4 遍历字典的所有键
favorite_languages = { # 字典
 'jen': 'python',
 'sarah': 'c',
 'edward': 'rust',
 'phil': 'python',
 }

friends = ['phil', 'sarah']

# for name in favorite_languages.keys():
#     if name in friends:
#         language = favorite_languages[name].title()
#         print(f"\nHi, {name.title()}, I see you love {language}!")
#
# if 'erin' not in favorite_languages.keys():
#     print("\nErin, please take our poll!")

# for name in sorted(favorite_languages.keys(), reverse=True):
#     print(f"{name.title()}, thank you for taking the poll.")

## 4.5 遍历字典的所有值
# for language in favorite_languages.values():
#     print(language.title())

# for language in set(favorite_languages.values()):
#  print(language.title())

languages = {'python', 'rust', 'python', 'c'} # 集合
# for language in sorted(languages):
#     print(language.title())

## 4.6 字典、列表嵌套
users = {
    'aeinstein': {
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
    },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
    },
}

# for username, user_info in users.items():
#     print(f"\nUsername: {username}")
#     full_name = f"{user_info['first']} {user_info['last']}"
#     location = user_info['location']
#     print(f"\tFull name: {full_name.title()}")
#     print(f"\tLocation: {location.title()}")